﻿Public Class Settings

    Sub New()
        InitializeComponent()
        LstBack.Groups.Add("g1", "Desenler")
        LstBack.Groups.Add("g2", "Düz renkler")
        LstBack.TileSize = New Size(50, 50)
        For i = 1 To 8
            Dim Item = LstBack.Items.Add("")
            Item.Name = "bg" & i
            Item.Tag = Item.Name
            LstBack.Groups("g1").Items.Add(Item)
        Next
        For Each c In From r In Enumerable.Range(0, 6) From g In Enumerable.Range(0, 6) From b In Enumerable.Range(0, 6) Order By Math.Max(Math.Max(r, g), b) - Math.Min(Math.Min(r, g), b), r, g, b
            Dim Col As Color = Color.FromArgb(255 - c.r * 51, 255 - c.g * 51, 255 - c.b * 51)
            Dim Item = LstBack.Items.Add("")
            Item.Tag = Col
            Item.Name = Col.ToString
            LstBack.Groups("g2").Items.Add(Item)
        Next
    End Sub

    Private Sub Background_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles LstBack.DrawItem
        Dim R As Rectangle = e.Item.Bounds
        If TypeOf e.Item.Tag Is Color Then
            e.Graphics.FillRectangle(New SolidBrush(e.Item.Tag), R)
        Else
            e.Graphics.DrawImage(my.Resources.ResourceManager.GetObject(e.Item.Tag), R, New Rectangle(New Point(0, 0), R.Size), GraphicsUnit.Pixel)
        End If
        If e.Item.Selected Then
            R.Width -= 1 : R.Height -= 1
            e.Graphics.DrawRectangle(New Pen(Color.FromArgb(128, Color.Black)), R)
            R.Inflate(-1, -1)
            e.Graphics.DrawRectangle(New Pen(Color.FromArgb(128, Color.White)), R)
        End If
    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        If LstBack.SelectedItems(0) IsNot Nothing Then
            Dim Item = LstBack.SelectedItems(0).Tag
            Background = If(TypeOf Item Is Color, CType(Item, Color).ToArgb, Item)
        End If
        ShowAlbumsInListview = CStr(ChkAlbumsInListView.Checked)
        AutoExpand = CStr(ChkAutoExpand.Checked)
        Save()
    End Sub

    Private Sub FrmSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Background <> "" Then LstBack.Items(Background).Selected = True
            ChkAlbumsInListView.Checked = ShowAlbumsInListview = "True"
            ChkAutoExpand.Checked = AutoExpand = "True"
        Catch ex As Exception
        End Try
    End Sub

    Public Shared Background As String
    Public Shared ShowAlbumsInListview As String
    Public Shared AutoExpand As String

    Public Shared Sub Open()
        Background = Settings("Background")
        ShowAlbumsInListview = Settings("ShowAlbumsInListview")
        AutoExpand = Settings("AutoExpand")
    End Sub

    Public Shared Sub Save()
        Settings("Background") = Background
        Settings("ShowAlbumsInListview") = ShowAlbumsInListview
        Settings("AutoExpand") = AutoExpand
    End Sub

    Public Shared Property Settings(Name As String)
        Get
            Return GetSetting("PhotoStudio", "Settings", Name)
        End Get
        Set(Value)
            SaveSetting("PhotoStudio", "Settings", Name, Value)
        End Set
    End Property
End Class